;(function(w){
var vm = new Vue({
    el:'#index',
    data:{
        activeName: null,
        activeIndex: '1',
        curentLoginInfo:null,
        clickTime:new Date().getTime(),
        menuCell :5, // 导航菜单的间隔像素
        videoListData:[],
        pageNum:1,
        pageSize:10,
        total:0,
        total2:0,
        keyword:'',
        tokenHead:'',
        token:'',
        topTabData: [{
            label: '首页',
            id: '1',
            url: './main/mainIndex.html',
            thUrl:'tzpt/main/toMainIndexPage',
            hash:'/'
        }, {
            label: '办事指南',
            id: '2',
            url: './guide/guideIndex.html',
            thUrl:'rest/guide/toGuideIndexPage',
            hash:'/guideIndex'
        }, {
            label: '项目公开',
            id: '3',
            url: './main/itemOpen.html',
            thUrl:'tzpt/main/toItemOpenPage',
            hash:'/itemOpen'
        }, {
            label: '政策法规',
            id: '4',
            url: './main/regulationIndex.html',
            thUrl:'tzpt/main/toRegulationIndexPage',
            hash:'/regulationIndex'
        },
            {
                label: '通知公告',
                id: '5',
                url: './main/toNoticePage.html',
                thUrl:'tzpt/main/toNoticePage',
                hash:'/toNoticePage'
            },
            {
                label: '中介机构',
                id: '6',
                url: './supermarket/main/index.html',
                thUrl:'supermarket/main/index.html',
                hash:'/supermarketIndex'
            }
        ]
    },
    created:function () {
        this.init();
        this.layuiInit();
    },
    mounted: function () {

    },
    methods:{
        init:function(){
            var vm = this;
            var user = sessionStorage.getItem("user");
            var header = sessionStorage.getItem("tokenHead");
            var token = sessionStorage.getItem("token");
            console.log(user);
            if(user!='undefined'&& user!=null){
                vm.curentLoginInfo = user;
                vm.tokenHead = header;
                vm.token = token
            }else {
                vm.layuiLogin();
                setTimeout(function () {
                    window.location.href = '/zzh/login/loginIndex.html';
                }, 1000)
            }
            vm.getVideoListData();
            //点击页面后查询页面是否五分钟没有操作  是则自动退出登录
            document.addEventListener('click',vm.eventListener())
        },
        getVideoListData:function(){
            var vm  = this;
            $.ajax({
                url:'/video/list',
                dataType:'json',
                type:'POST',
                headers:{'Authorization':vm.tokenHead+vm.token},
                data:{
                    keyword:vm.keyword,
                    pageNum:vm.pageNum,
                    pageSize:vm.pageSize
                },
                success:function (data) {
                    if(data.success){
                        vm.videoListData = data.content.list;
                        vm.total = data.content.total;
                    }
                },
                error:function () {
                    vm.$message.error('获取视频数据失败！');
                }
            })
        },
        showVideo:function(id){
            console.log(id);
            window.open("/video/openVideo/"+id,"_blank");
        },
        //事件监听器
        eventListener:function(){
            var vm = this;
            if(new Date().getTime()-vm.clickTime>5*60*1000){
                document.removeEventListener('click',vm.eventListener);
                layer.alert("登陆超时，请重新登录",{icon:3,title:"提示",closeBtn:0,skin:'layui-layer-molv'},function () {
                    sessionStorage.removeItem("user");
                    window.location.href='loginIndex.html';
                });
            }
        },
        //退出登录
        loginOut:function(){
            sessionStorage.removeItem("user");
           this.layuiOut();
           setTimeout(function () {
               window.location.href='loginIndex.html';
           },1000)
        },
        //layui初始化
        layuiInit:function(){
            var vm = this;
            layui.use(['laypage', 'layer','carousel','element'], function(){
                var laypage = layui.laypage
                    ,layer = layui.layer;
                var carousel = layui.carousel;
                var element = layui.element;
                var $ = layui.jquery;
                //建造实例
                carousel.render({
                    elem: '#pic'
                    ,width: '100%' //设置容器宽度
                    ,height:'350px'
                    ,arrow: 'always' //始终显示箭头
                    //,anim: 'updown' //切换动画方式
                    ,interval: 3000
                });
                //完整功能
                laypage.render({
                    elem: 'demo'
                    ,count: vm.total
                    ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                    ,jump: function(obj){
                        console.log(obj)
                    }
                });
                laypage.render({
                    elem: 'demo2'
                    ,count: vm.total2
                    ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                    ,jump: function(obj){
                        console.log(obj)
                    }
                });
                //获取所有的菜单
                request('',{
                    type: "GET",
                    url: "/common/json/header.json",
                    dataType: "json",
                },function (res) {
                    //重点地方，将Layui在页面加载时渲染出来的span.layui-nav-bar提前删除掉
                    //因为在页面打开的时候进行了固定的渲染，然后通过重新加载不能够重置掉.layui-nav-bar，
                    // 所以只能将带有这个class的内容全删除，让框架识别它是没渲染过的然后再通过方法重新渲染才能有效
                    $("#nav").find('span.layui-nav-bar').remove();
                    var data = res;
                    console.log("头部导航栏"+data);
                    var liStr = "";
                    // 遍历生成主菜单
                    for (var i = 0; i < data.length; i++) {
                        // 判断是否存在子菜单
                        if (data[i].childMenus != null && data[i].childMenus.length > 0) {
                            liStr += "<li class=\"layui-nav-item\" style=\'animation:titles 1s;-webkit-animation:titles 1s;\'><a class=\"\" href=\"javascript:;\" data-src=\"" + data[i].url + "\" data-menuid=\"" + data[i].menuId + "\" data-title=\"" + data[i].name + "\"><svg class=\""+"icon"+"\"><use xlink:href=\"#"+data[i].pic+"\"></use></svg>" + data[i].name + "</a>\n" +
                                "<dl class=\"layui-nav-child\">\n";
                            // 遍历获取子菜单
                            for (var k = 0; k < data[i].childMenus.length; k++) {
                                liStr += vm.getChildMenu(data[i].childMenus[k], 0);
                            }
                            liStr += "</dl></li>";
                        } else {
                            if(data[i].name=='首页'){
                                liStr += "<li class=\"layui-nav-item layui-this\" style=\'animation:titles 0.666667s;-webkit-animation:titles 0.666667s;\'><a class=\"site-demo-active\" data-src=\"" + data[i].url + "\" data-menuid=\"" + data[i].menuId + "\" data-title=\"" + data[i].name + "\"><svg class=\""+"icon"+"\"><use xlink:href=\"#"+data[i].pic+"\"></use></svg>" + data[i].name + "</a></li>";
                            }else{
                                liStr += "<li class=\"layui-nav-item\" style=\'animation:titles 0.666667s;-webkit-animation:titles 0.666667s;\'><a class=\"site-demo-active\" data-src=\"" + data[i].url + "\" data-menuid=\"" + data[i].menuId + "\" data-title=\"" + data[i].name + "\"><svg class=\""+"icon"+"\"><use xlink:href=\"#"+data[i].pic+"\"></use></svg>" + data[i].name + "</a></li>";
                            }

                        }
                    };
                    $(".topMenu").html(liStr);
                    element.init();
                })
            });
        },
        getChildMenu:function(subMenu, num){
            var vm = this;
            // 递归生成子菜单
            num++;
            var subStr = "";
            if (subMenu.childMenus != null && subMenu.childMenus.length > 0) {
                subStr += "<dd><ul><li class=\"layui-nav-item\" ><a style=\"margin-Left:" + num * vm.menuCell + "px\" class=\"site-demo-actives\" href=\"javascript:;\" data-src=\"" + subMenu.url + "\" data-menuid=\"" + subMenu.menuId + "\" data-title=\"" + subMenu.name + "\">" + subMenu.name + "</a>" +
                    "<dl class=\"layui-nav-child\">\n";
                for (var j = 0; j < subMenu.childMenus.length; j++) {
                    subStr += vm.getChildMenu(subMenu.childMenus[j], num);
                }
                subStr += "</dl></li></ul></dd>";
            } else {
                subStr += "<dd><a style=\"margin-Left:" + num * vm.menuCell + "px\" class=\"site-demo-active\" data-src=\"" + subMenu.url + "\" data-menuid=\"" + subMenu.menuId + "\" data-title=\"" + subMenu.name + "\">" + subMenu.name + "</a></dd>";
            }
            return subStr;
        },
        layuiOut:function(){
            layui.use('layer', function(){
                layer.msg("退出成功",{icon: 1,time:1000});
            });
        },
        layuiLogin:function(){
            layui.use('layer', function(){
                layer.msg("请先登录！",{icon: 1,time:1000});
            });
        },
        handleSelect:function(key, keyPath) {
            console.log(key, keyPath);
        },
        routerChange:function(hash,url,thUrl,index) {
            if(index===5){
                w.open(ctx+thUrl);
            }else{
                this.activeName = index;
                // var res = document.getElementById('app_frame2');
                //地址栏改变
                w.location.hash = hash;
                w.location.search = '';
            }
        }
    }
})
})(typeof window === 'undefined'? this : window);